package com.gopay.bis.ccb.ebank.operation;

import org.apache.commons.lang3.StringUtils;

import CCBSign.RSASig;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.DateUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.CcbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 建行B2B签名验签实现类
 * 
 * @CCBCerthelper.java
 * @author WangHongFei
 */
public class CcbB2bCerthelper implements ICertHelper {
	
	private RSASig rsaHelperB2b = null;
	private B2bSocketTransportImpl b2bSocketTransportImpl;
	private CcbB2bBuildMessage ccbB2bBuildMessage;
	private CcbB2bParseMessage ccbB2bParseMessage;

	public String sign(BaseMessage message) throws GopayException{		
		try {
			CcbB2bMessage b2bMessage = (CcbB2bMessage) message;
			rsaHelperB2b.setPublicKey(b2bMessage.getPublicKeyPath());
			BankLogUtils.printB2BSignPlainLog(b2bMessage);
			String digest = rsaHelperB2b.generateDigest(b2bMessage.getPlainMessage());
			BankLogUtils.printB2BSignSignatureLog(b2bMessage, digest);
			return digest;
		} catch (Exception e) {
			BankLogUtils.printB2BSignExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6028);
		}
		
	}

	public Object verify(BaseMessage message) throws GopayException{		
		try{
			CcbB2bMessage b2bMessage = (CcbB2bMessage) message;
			rsaHelperB2b.setPublicKey(b2bMessage.getPublicKeyPath());
			BankLogUtils.printB2BVerifyPlainLog(b2bMessage);
			BankLogUtils.printB2BVerifySignatureLog(b2bMessage);
			boolean verifyRes = rsaHelperB2b.verifySigature(b2bMessage.getSignMessage(),b2bMessage.getPlainMessage());
			if(verifyRes==false){
				b2bMessage.setLanguage("CN");// 语言
		    	// 请求序列号
				b2bMessage.setRequestSn(TransUtils.getPackageID());
				// 交易码
				b2bMessage.setTxCode(CcbConstants.MERCHANT_PAY_QUERY);
				// 页次
				b2bMessage.setPage("1");
				b2bMessage.setPoscode("582137706");// 外借pos机号
				b2bMessage.setStatus("3"); // 订单流水状态(0:交易失败,1:交易成功,2:待银行确认(针对未结流水查询);3:全部)
				/* 按订单号查询时，时间段不起作用 */
				b2bMessage.setStartDate(DateUtils.date2string(DateUtils.now(), "yyyyMMdd"));
				b2bMessage.setEndDate(DateUtils.date2string(DateUtils.now(), "yyyyMMdd"));
				b2bMessage.setKind("0");
				b2bMessage.setOrder(b2bMessage.getOrderId());
				 String bulidMsg = ccbB2bBuildMessage.buildB2bXmlStr(b2bMessage);// 构建xml
		         Object obj = this.b2bSocketTransportImpl.submit(bulidMsg); // 发送银行
		         CcbB2bMessage retMsg = ccbB2bParseMessage.parseB2bXml(obj);// 解析银行返回参数
		         if (StringUtils.equals(CcbConstants.CCB_SUCCESS,retMsg.getReturnCode())){
		        	if(retMsg.getStatus().equals(CcbConstants.B2C_QUERY_SUCC)){
		        		verifyRes=true;
		        		b2bMessage.setStatus("2");
		        	}
		         }
			}
			BankLogUtils.printB2BVerifyResultLog(message, verifyRes);
			return verifyRes;
		}catch(Exception e){
			BankLogUtils.printB2BVerifyExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6030);
		}	
	}
	
	public void setrsaHelperB2b(RSASig rsaHelperB2b) {
		this.rsaHelperB2b = rsaHelperB2b;
	}

	public void setB2bSocketTransportImpl(
			B2bSocketTransportImpl b2bSocketTransportImpl) {
		this.b2bSocketTransportImpl = b2bSocketTransportImpl;
	}

	public void setCcbB2bBuildMessage(CcbB2bBuildMessage ccbB2bBuildMessage) {
		this.ccbB2bBuildMessage = ccbB2bBuildMessage;
	}

	public void setCcbB2bParseMessage(CcbB2bParseMessage ccbB2bParseMessage) {
		this.ccbB2bParseMessage = ccbB2bParseMessage;
	}

}
